// pages/commentList/commentList.js
import * as API_Comment from '../../api/comment'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentList: [],
    storeId: null,
    params: {
      storeId: null,
      productId: null,
      pageNum: 1,
      pageSize: 10,
      pid: 0
    },
    starList: [, , , , , ],
    finished: false
  },
  handleManage (e) {
    let index = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    let commentItem = this.data.commentList[index]
    let id = commentItem.commentId
    let status = false
    let message = ''
    if (type === 'like') {
      status = commentItem.is_like
      message = '点赞成功'
    }
    if (type === 'collect') {
      status = commentItem.is_collect
      message = '收藏成功'
    }
    message = status ? '取消' + message : message
    let url = status ? '/comment/delCommentRelatedType' : '/comment/createCommentRelatedType'
    getApp().https.ajax({
      method: 'post',
      url: url,
      data: {
        commentId: id,
        type: type
      },
      loading: true
    }).then(() => {
      wx.showToast({
        title: message,
        icon: 'none'
      })
      if (type === 'like') {
        this.setData({
          [`commentList[${index}].is_like`]: !status,
          [`commentList[${index}].likeNum`]: status ? commentItem.likeNum - 1 : commentItem.likeNum + 1
        })
      }
      if (type === 'collect') {
        this.setData({
          [`commentList[${index}].is_collect`]: !status,
          [`commentList[${index}].collectNum`]: status ? commentItem.collectNum - 1 : commentItem.collectNum + 1
        })
      }
    })
  },
  toDetail(e) {
    wx.navigateTo({
      url: `../commentDetail/commentDetail?commentId=${e.currentTarget.dataset.id}&storeid=${this.data.params.storeId}&productId=${this.data.params.productId}`
    })
  },
  autoplayVideo: function (e) {
    let indexAll = e.currentTarget.dataset.indexAll;
    let commentList = this.data.commentList;
    commentList[indexAll].autoplay = true;
    this.setData({
      commentList: commentList
    });
    // debugger
  },
  preview(event) {
    let currentUrl = event.currentTarget.dataset.src
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: event.currentTarget.dataset.srcs // 需要预览的图片http链接列表
    })
  },
  getlist: function () {
    if (this.data.params.finished) {
      return
    }


    API_Comment.getList(this.data.params).then(res => {
      let arr = this.data.commentList

      for (let item of res.data.rows) {

        let arr = [];
        if (item.commentImages) {
          arr = item.commentImages.split(",");
        }

        let arrvideo = [];
        if (item.commentVideo) {
          arrvideo = item.commentVideo.split(",");
        }

        item.commentImages = arr
        item.commentVideo = arrvideo
        item.storeLevel = item.storeLevel.toFixed(1)

      }
      arr.push(...res.data.rows)
      this.setData({
        commentList: arr,
        finished: res.data.lastPage
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let productId = options.productId ? options.productId : '';
    this.setData({
      'params.storeId': options.storeid,
      'params.productId': productId,
    })
    this.getlist();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.finished) {
      return
    }
    var page = this.data.params.pageNum + 1
    this.setData({
      'params.pageNum': page
    })
    this.getlist()
  },
})